var pai_se = {0:'<font color="#000">黑</font>',1:'<font color="red">红</font>',2:'<font color="#000">梅</font>',3:'<font color="red">方</font>',4:'王'}
var show_pai = ['A','2','3','4','5','6','7','8','9','10','J','Q','K','小王','大王']
var B={/*桌面*/
	init:function(option){
		var opt=jQuery.extend({
			isOnline:false
		},option);
		$('div.paiarea').empty()
		$('div#publicarea').empty()
		$('div.resultarea').empty()
		return {
			dead:new Array(),
			isOnline:opt.isOnline,
		}
	}
}
var P={/*牌 暂时定为1副牌（2-6人）*/
	last_pai:new Array(),/*牌堆中最后剩下的牌*/
	init_pai:function(){
		var a = []
		for(var i=0;i<54;i++){a[i]=i;}
		this.last_pai = a
	},
	init:function(){
		this.init_pai();
		return {
			last:this.last_pai,
			temp:[],/*用来缓存打出的牌*/
			presentPai:[],/*当轮打出的牌*/
			outPai:function(){/*从牌堆里出牌*/
				var out='';
				if(this.last.length>0){
					var out_index=_getRandom(this.last.length)
					out = this.last[out_index]
					this.last.splice(out_index,1)
				}else{}
				this.writeLastNum()
				//this.printLastPai()
				return out;
			},
			writeLastNum:function(){
				$('#last_pai_num').text(parseInt(this.last.length))
			},
			addPai:function(val){/*向牌堆中增牌*/
				this.last.push(parseInt(val))
				this.printLastPai()
			},
			outNumsPai:function(nums){/*从牌堆中弹出指定张数的牌*/
				var out = new Array();
				if(this.last.length<=nums){
					this.last = this.last.concat(this.temp)
					this.temp = []
				}
				for(var i=0;i<nums;i++){
					out[i]=this.outPai();
				}
				out.sort(function(a,b){return a%13-b%13;})
				return out;
			},
			addSignPai:function(pai){/*向牌堆中添加指定的牌 @pai 值类似‘2，4，6，12，17’*/
				var p = pai.split(',')
				for(var i=0;i<p.length;i++){
					this.addPai(p[i]);
				}
			},
			checkPaiType:function(pai){
				/*检测牌的类型 1:攻击牌;2:防守牌;3:普通牌;4:可攻可守*/
				var attack = [10,11,12];
				var guard = [1,2];
				var general = [3,4,5,6,7,8,9];
				var att_gua = [0,13,14]
				if(attack.indexOf(pai)!=-1){return 1;}
				if(guard.indexOf(pai)!=-1){return 2;}
				if(general.indexOf(pai)!=-1){return 3;}
				if(att_gua.indexOf(pai)!=-1){return 4;}
			},
			checkAttack:function(pai){ /*检测牌是否为攻击牌*/
				pai = parseInt(pai)
				var attack = [10,11,12,0,13,14]
				if(attack.indexOf(pai)!=-1){
					return true;
				}
				return false;
			},
			allAttack:function(pai){/*检测给到的所有牌是否为攻击牌*/
				if(typeof pai != "object" || pai.length<1){return false;}
				var ret = true;
				for(var i=0;i<pai.length;i++){
					if(this.checkAttack(pai[i])){continue;}
					ret = false;
				}
				return ret;
			},
			getAllAttack:function(pai){ //返回指定牌中所有的攻击牌
				if(typeof pai != "object" || pai.length<1){return false;}
				var retPai=new Array();
				var retVal=new Array();
				var paival;
				for(var i=0;i<pai.length;i++){
					paival = this.valToPai(pai[i])
					if(this.checkAttack(paival)){
						retPai.push(pai[i]);
						retVal.push(paival);
					}
				}
				if(retPai.length<1 || retVal.length<1){return false;}
				return [retPai,retVal];
			},
			getOnlyAttack:function(pai){ //返回指定牌中的纯攻击牌
				if(typeof pai != "object" || pai.length<1){return false;}
				var retPai=new Array();
				var retVal=new Array();
				var onlyAttack = [0,10,11,12]
				for(var i=0;i<pai.length;i++){
					var paival = this.valToPai(pai[i])
					if(onlyAttack.indexOf(paival)!=-1){
						retPai.push(pai[i]);
						retVal.push(paival);
					}
				}
				if(retPai.length<1 || retVal.length<1){return false;}
				return [retPai,retVal];
			},
			getBastAttack:function(pai,nums){/*返回最佳的攻击牌组合*/
				if(typeof pai != "object" || pai.length<1){return false;}
				var retpai = new Array();
				var retval = new Array();
				var allAttack = this.getOnlyAttack(pai)
				if(allAttack){
					switch(nums){
						case 1:
							if(allAttack[1].length>0){
								retpai.push(allAttack[0][0])
								retval.push(allAttack[1][0])
								flag = false;
							}
							break;
						case 2:
							var dbp = this.getDouble(allAttack)
							try{
								for(var i in dbp){
									var index = parseInt(i)
									retpai = [dbp[i][0],dbp[i][1]]
									retval = [index,index]
								}
							}catch(e){}
							break;
						default:
							var dbp = this.getPaivalTime(allAttack)
							try{
								for(var i in dbp){
									var index = parseInt(i)
									if(dbp[i][0]>=nums){
										for(var j=0;j<nums;j++){
											retpai.push(dbp[i][1][j])
											retval.push(index)
										}
									}
								}
							}catch(e){}
							break;
					}
				}
				if(retpai.length<1 || retval.length<1){return false;}
				return [retpai,retval];
			},
			checkGuard:function(pai){/*检测牌是否为防守牌*/
				var attack = [1,2,0,13,14]
				if(attack.indexOf(pai)!=-1){
					return true;
				}
				return false;
			},
			getBastGuard:function(pai,lose){//根据被砍刀数确定最佳防守组合  pai的值为牌的原型
				var allguard = this.getAllGuard(pai);
				var retpai = new Array();
				var retval = new Array();
				if(!allguard || allguard[0].length<1){ //玩家手牌中不存在防守牌 返回false
					return false;
				}
				var comb = this.combPai(allguard,lose)
				if(comb){
					retpai=comb[0]
					retval=comb[1]
				}
				if(retpai.length<1 || retval.length<1){
					return false;
				}
				return [retpai,retval]
			},
			allGuard:function(pai){/*检测给到的所有牌是否为防守牌*/
				if(typeof pai != "object" || pai.length<1){return false;}
				var ret = true;
				for(var i=0;i<pai.length;i++){
					if(this.checkGuard(pai[i])){continue;}
					ret = false;
				}
				return ret;
			},
			getAllGuard:function(pai){/*返回pai中的所有防守牌*/
				if(typeof pai != "object" || pai.length<1){return false;}
				var paitemp = new Array();
				var paival = new Array();
				for(var i=0;i<pai.length;i++){
					var temp = pai[i]
					var val = (pai[i]/13)>=4?(pai[i]%13)+13:pai[i]%13;
					if(this.checkGuard(val)){
						paitemp.push(temp)
						paival.push(val)
					}
				}
				if(paitemp.length<1 || paival.length<1){
					return false;
				}
				return [paitemp,paival];
			},
			haveGuard:function(pai){/*检测给到的所有牌是否含有纯防守牌*/
				if(typeof pai != "object" || pai.length<1){return false;}
				var ret = false;
				var temp = [1,2];
				for(var i=0;i<pai.length;i++){
					if(temp.indexOf(pai[i])==-1){
						continue;
					}
					ret = true;
				}
				return ret;
			},
			checkGeneral:function(pai){/*检测牌是否为普通牌*/
				var attack = [3,4,5,6,7,8,9]
				if(attack.indexOf(pai)!=-1){
					return true;
				}
				return false;
			},
			getBastGeneral:function(pai,preval,nums){/*检测给到的所有牌是否为普通牌*/
				if(typeof pai != "object" || pai.length<1){return false;}
				var retpai = new Array();
				var retval = new Array();
				var allGeneral = this.getAllGeneral(pai)
				var dbp = this.getPaivalTime(allGeneral) //返回牌中各值出现的次数
				if(allGeneral){
					switch(nums){
						case 1:
							var flag = false;
							var i = 0;
							do{
								if(allGeneral[1][i]>preval && !dbp[allGeneral[1][i]]){
									retpai.push(allGeneral[0][i])
									retval.push(allGeneral[1][i])
									flag = false;
								}else{
									flag = true;
								}
							}while(flag && ++i<allGeneral[1].length)
							break;
						case 2:
							var dbp = this.getDouble(allGeneral)
							try{
								for(var i in dbp){
									var index = parseInt(i)
									if(i>preval){
										retpai = [dbp[i][0],dbp[i][1]]
										retval = [index,index]
									}
								}
							}catch(e){}
							break;
						case 3:
						case 4:
						case 5:
							try{
								for(var i in dbp){
									var index = parseInt(i)
									if(dbp[i][0]>=nums && index>paival){
										for(var j=0;j<nums;j++){
											retpai.push(dbp[i][1][j])
											retval.push(index)
										}
									}
								}
							}catch(e){}
							break;
						default:
							try{
								for(var i in dbp){
									var index = parseInt(i)
									if(retval.length<1 && retpai.length<1){
										for(var j=0;j<dbp[i][0];j++){
											retval[j] = index
										}
										retpai = dbp[i][1]
									}
								}
							}catch(e){}
							break;
					}
				}
				if(retpai.length<1 || retval.length<1){return false;}
				return [retpai,retval];
			},
			getDouble:function(pais){ //得到指定牌中成对的牌 并返回
				if(typeof pais!='object' || pais.length<1){return {};}
				var temp=undefined,paival = pais[1];
				var retkey = new Object();
				var temp = paival[0],flag=0;
				for(var i=1;i<paival.length;i++){
					if(paival[i]==temp){
						if(typeof retkey[temp] == 'object'){
							retkey[temp].push(pais[0][i])
						}else{
							retkey[temp]=[pais[0][i-1],pais[0][i]]
						}
					}else{
						temp = paival[i]
						flag++
					}
				}
				return retkey
			},
			getPaivalTime:function(pais){ //得到指定牌中各值出现的次数
				if(typeof pais!='object' || pais.length<1){return {};}
				var temp,ret={};
				var a = pais[1]
				for(var i in a){
					if(temp==a[i]){
						if(ret[a[i]]){
							ret[a[i]][0]+=1;
							ret[a[i]][1].push(pais[0][i])
						}else{
							ret[a[i]]=new Array();
							ret[a[i]][0]=2;
							ret[a[i]][1]=[pais[0][i-1],pais[0][i]]
						}
					}else{
						temp=a[i];
					}
				}
				return ret;
			},
			allGeneral:function(pai){/*检测给到的所有牌是否为普通牌*/
				if(typeof pai != "object" || pai.length<1){return false;}
				var ret = true;
				for(var i=0;i<pai.length;i++){
					if(this.checkGeneral(pai[i])){continue;}
					ret = false;
				}
				return ret;
			},
			getAllGeneral:function(pai){//返回牌中所有的普通牌
				if(typeof pai != "object" || pai.length<1){return false;}
				var paitemp = new Array();
				var paival = new Array();
				for(var i=0;i<pai.length;i++){
					var temp = pai[i]
					var val = (pai[i]/13)>=4?(pai[i]%13)+13:pai[i]%13;
					if(this.checkGeneral(val)){
						paitemp.push(temp)
						paival.push(val)
					}
				}
				if(paitemp.length<1 || paival.length<1){
					return false;
				}
				return [paitemp,paival];
			},
			printLastPai:function(){
				if($('#showLastPai').length<1){
					$('body').prepend('<div id="showLastPai">'+this.last.join()+'</div>')
				}else{
					$('#showLastPai').html('<div id="showLastPai">'+this.last.join()+'</div>')
				}
			},
			paiToLose:function(pai){
				var pto = {
					'0':1,
					'1':2,
					'2':3,
					'10':1,
					'11':2,
					'12':3,
					'13':3,
					'14':3
				}
				return pto[pai]
			},
			haveFive:function(pai){
				var temp = pai
				return temp[0]==temp[temp.length-1]
			},
			valToPai:function(val){
				if(typeof val == 'object'){
					var ret = new Array();
					for(var i=0;i<val.length;i++){
						ret.push(this.valToPai(val[i]))
					}
					return ret;
				}
				if(val/13>=4){
					return val%13+13;
				}
				return val%13;
			},
			getPaiHuase:function(val){
				if(val/13>=4){
					if(val%13==0){return 4;}else{return 5;}
				}
				return parseInt(val/13);
			},
			combPai:function(pais,lose){
				if(typeof pais!='object' || pais.length<1){return false;}
				var tpai = pais[0]
				var val = pais[1]
				var retpai = new Array(),retval = new Array();
				var sum = 0,prev = 0;
				
				for(var i=0;i<val.length;i++){
					if(val[i]==13||val[i]==14){
						val[i] = 2;
					}
				}
				if(lose<4){
					var i=0;
					do{
						if(val[i]>=lose-1){
							retpai = [tpai[i]]
							retval = [val[i]]
							i=val.length
						}
					}while(++i<val.length)
				}
				if(retpai.length>0 && retval.length>0){return [retpai,retval];}

				if(val.length<=1 || lose<val[0]+1){
					retpai = [tpai[0]]
					retval = [val[0]]
				}else{
					for(var i=0,j=val.length-1;i<j;){
						if(val[i]==13||val[i]==14){val[i]=2;}
						if(val[j]==13||val[j]==14){val[j]=2;}
						sum=val[i]+val[j]+2;
						if(prev>lose && sum<lose){
							var m = prev-lose
							var n = lose-sum
							if(m>=n){
								retpai = [tpai[i],tpai[j]]
								retval = [val[i],val[j]]
								i=j;
							}else{
								retpai = [tpai[i],tpai[j+1]]
								retval = [val[i],val[j+1]]
								i=j;
							}
						}
						if(prev<lose && sum>lose){
							var m = lose-prev
							var n = sum-lose
							if(m>=n){
								retpai = [tpai[i],tpai[j]]
								retval = [val[i],val[j]]
								i=j;
							}else{
								retpai = [tpai[i-1],tpai[j]]
								retval = [val[i-1],val[j]]
								i=j;
							}
						}
						if(prev<lose && sum<lose){
							var m = lose-prev
							var n = lose-sum
							if(m>=n){
								retpai = [tpai[i],tpai[j]]
								retval = [val[i],val[j]]
								i=j;
							}else{
								retpai = [tpai[i-1],tpai[j]]
								retval = [val[i-1],val[j]]
								i=j;
							}
						}
						if(sum>lose){j--;}
						if(sum<lose){i++;}
						if(lose == sum){
							retpai = [tpai[i],tpai[j]]
							retval = [val[i],val[j]]
							i=j;
						}
					}
				}
				if(retpai.length<1 || retval.length<1){return false;}
				return [retpai,retval]
			},
			combArray:function(pais,nums){//数组组合结果
				if(typeof pais!='object' || pais.length<1){return {};}
				var retpai = pais[0]
				var retval = pais[1]
				var retobj = new Object();
				var temp_index=new Array(),temp_comb = new Array();
				var len = retval.length
				for(var i=0;i<len;i++){
					if(i<nums){
						temp_comb[i] = 1
						temp_index.push(i)
					}else{
						temp_comb[i] = 0
					}
				}
				//console.log(temp_comb);
				var flag = 0,actflag=0;
				for(var i=1;i<len;){
					if(1==temp_comb[i] && 0==temp_comb[i+1]){
						/*处理返回结果*/
						var sum=0;
						var temp_paival = new Array(),temp_pai = new Array();
						for(var j=0;j<len;j++){
							if(1==temp_comb[j]){
								sum+=retval[j];
								temp_pai.push(retpai[j]);
								temp_paival.push(retval[j]);
							}
						}
						if(!retobj[sum]){
							retobj[sum] = new Array();
						}
						retobj[sum].push([temp_pai,temp_paival])
						/*处理结束*/
						temp_comb[i]=0;
						temp_comb[i+1]=1;
						if(i>=len-2){actflag+=1;len-=1;}
						if(nums==++flag){flag=0}
						if(flag==1){
							for(var j=0;j<temp_comb.lastIndexOf(1);j++){
								if(j<nums-actflag-1){temp_comb[j]=1;}else{temp_comb[j]=0;}
							}
						}
						i=0;
						continue;
					}else{
						i++;
					}
				}
			}
		}
	}
}
/*
	if(1==temp_comb[i] && 0==temp_comb[i+1]){
						/*处理返回结果
						var sum=0;
						var temp_paival = new Array(),temp_pai = new Array();
						for(var j=0;j<len;j++){
							if(1==temp_comb[j]){
								sum+=retval[j];
								temp_pai.push(retpai[j]);
								temp_paival.push(retval[j]);
							}
						}
						if(!retobj[sum]){
							retobj[sum] = new Array();
						}
						retobj[sum].push([temp_pai,temp_paival])
						/*处理结束
						temp_comb[i]=0;
						temp_comb[i+1]=1;
						if(nums==++flag){flag=0}
						i=0;
						continue;
					}else{
						i++;
					}
	
*/
var G={/*玩家*/
	init:function(option){
		var opt=jQuery.extend({
			id:undefined,
			pai:[],
			name:'玩家',
			image:'static/images/init.jpg'
		},option);
		opt.pai.sort(function(a,b){return a%13-b%13;})
		return {
			id:opt.id,
			pai:opt.pai,
			name:opt.name,
			image:opt.image,
			automode:false,
			life:13,
			limit:3,
			select:[],/*玩家当前选择的牌*/
			lastout:[],/*最终选择的牌 原始牌 0-53*/
			paival:[],/*最终打出的牌 经过处理的牌 A-K*/
			nextgamer:undefined,/*下家id*/
			prevgamer:undefined,/*上家id*/
			lose:0,
			deal:function(val){
				if(!val || val.length<1)return false;
				/*抓牌 @val为此次得到的牌 类型为数组[]*/
				for(var v in val){
					if(this.pai.length<5){
						this.pai.push(parseInt(val[v]));
					}else{
						//alert('玩家'+this.id+'的手牌已经足够')	
					}
				}
				this.pai.sort(function(a,b){return a%13-b%13;})
			},
			play:function(value){
				/*出牌 @value为此次打出的牌 类型为数组[]*/
				if(this.pai.length>0){
					for(var val in value){
						var index = this.pai.indexOf(value[val])
						if(index!=-1){
							this.pai.splice(index,1)
						}else{
							var ind = value[val]%13
							if(value[val]/13>=4){
								ind+=13
							}
							alert('玩家'+this.id+'手中没有牌:'+show_pai[ind])	
						}
					}
				}else{
					alert('玩家'+this.id+'已无手牌')	
				}
			},
			ponder:function(){
				/*电脑自动出牌规则*/
			},
			init:function(){
				this.lastout=[];
				this.paival=[];
				this.select=[];
				this.nextgamer=undefined;
				this.prevgamer=undefined;
				this.lose=0;
			},
			checkLose:function(){
				return this.lose;
			},
			getLife:function(){
				return this.life;
			}
		}
	}
}

var CGI = {/*服务器交互接口*/
	key:undefined,
	cgi:undefined,
	init:function(){
		if(!this.cgi){
			this.setKey();
			this.setCgi();
		}
		return this.cgi;
	},
	setKey:function(){
		this.key='abc'
	},
	setCgi:function(){
		this.cgi = this
	},
	putError:function(msg){
		if(msg){
			var data=new Object();
			data['m']=100;
			data['msg']=msg;
			this.ajaxPost(data);
		}
	},
	ajaxPost:function(data,callback){
		data['k']=this.key;
		try{
			$.ajax({
				url:'cgi/cgi.php',
				data:data,
				type:'POST',
				dateType:'json',
				success:function(e){callback?callback(e):''}
			})
		}catch(e){}
	}
}